<?php
if (!defined('CURRENT_PATH')){
	die('Hacking attempt');
	}
if($_REQUEST['act']&&$_REQUEST['act']=='saveAction'){
	//dump($this->spArgs());
/*Pics Of Latest Programme OR Pic Of Recommend Page******************************************************************/
if($this->spArgs(article_classify)==1){
$imgArr=explode(',',$this->spArgs(article_upload_img));
$imgToDel=explode(',',$this->spArgs(article_delete_img));	
$imgArr=(array_diff($imgArr,$imgToDel));
if($imgToDel){
foreach($imgToDel as $v){
	@unlink (APP_PATH.'/upload/image/'.$v);
	@unlink (APP_PATH.'/upload/image/'.preg_replace('/(\d+)\//U','\1/thumb/',$v));
}
}
if($imgArr){
$SummaryStr=null;
$SummaryStr=implode(',',array_filter($imgArr));	
unset($imgArr,$imgToDel);
}
}else{
$SummaryStr=$this->spArgs(article_brief);
$SummaryStr=!empty($SummaryStr)?$SummaryStr:null;	
}
/********************************************************************************************/
	$ps=spClass(posts);
	$postId=0;
if($this->spArgs(postId)!=null){
	$postId=intval($this->spArgs(postId));
	$umeta_id=$ps->find($conditions=array('id'=>$postId), $sort = null, $fields ='umeta_id');
	$umeta_id=$umeta_id[umeta_id];
	}			
/*****************************Meta 2012.02.28***********************************************/	
	if($this->spArgs(meta_keywords)!=null&&$this->spArgs(meta_description)!=null){
		$saveMapArr=array('meta_keywords'=>'meta_key','meta_description'=>'meta_des');
		$ms=spClass(site_meta);
		if($umeta_id!=0){
			$row=$this->DataToSave($saveMapArr,$this->spArgs(),array());
			$ms->update($conditions=array('umeta_id'=>$umeta_id),$row);
			$umeta_id=$umeta_id;
		}else{
			$newrow=$this->DataToSave($saveMapArr,$this->spArgs(),array('umeta_id'=>''));
			$ms->create($newrow);
			$umeta_id= mysql_insert_id();
		}
		unset($ms);
	}else{
		if($umeta_id!=0){
			$ms=spClass(site_meta);
			$ms->delete($conditions=array('umeta_id'=>$umeta_id));
			unset($ms);
		}
		$umeta_id=0;//equal zero refer both  meta have not writen
	}
/*******************************Posts 2012.02.28******************************************/
		$showOrder=intval($this->spArgs('show_order'));
		$showOrder=$showOrder>=0?$showOrder:0;//set the minimum  show order of a post
		$showOrder=$showOrder<=9999?$showOrder:9999;//set the maximum  show order of a post
		if($postId!=0){//update 
			if($this->spArgs(article_classify)==0){//single page
				$saveMapArr=array('title'=>'post_title','article_content'=>'post_datil',);
				$row=$this->DataToSave($saveMapArr,$this->spArgs(),array('umeta_id'=>$umeta_id,'post_author'=>$_SESSION['adminUserName'],'post_summary'=>$SummaryStr,'post_date'=>time()));
			}else{//other page
				$saveMapArr=array('title'=>'post_title','article_classify'=>'fid','article_content'=>'post_datil');
				$row=$this->DataToSave($saveMapArr,$this->spArgs(),array('sid'=>$showOrder,'umeta_id'=>$umeta_id,'post_author'=>$_SESSION['adminUserName'],'post_summary'=>$SummaryStr,'post_highlight'=>0,'post_satus'=>1,'post_date'=>time()));			
			}
			$ps->update($conditions=array('id'=>$postId),$row);	
			header("Content-Type:text/html;charset=utf-8");
			echo'<script> alert("恭喜，文章页面修改成功！");parent.location.reload( );</script>';
		}else{//Add A New One
			$saveMapArr=array('title'=>'post_title','article_classify'=>'fid','article_brief'=>'post_summary','article_content'=>'post_datil',);
			$newrow=$this->DataToSave($saveMapArr,$this->spArgs(),array('sid'=>$showOrder,'post_author'=>$_SESSION['adminUserName'],'umeta_id'=>$umeta_id,'post_date'=>time()));
			$ps->create($newrow);
			$this->sucess('恭喜！文章添加成功！');
		}	
		unset($ps);
/******************************************************************************************/	
	
			
}else if($_REQUEST['act']&&$_REQUEST['act']=='editframe'){//显示iframe
	$this->id=intval($_REQUEST['id']);
	$this->display('management/article_frame.html');
}else if($_REQUEST['act']&&$_REQUEST['act']=='modify'){//显示iframe
	$id=intval($_REQUEST['id']);
	$ps=spClass(posts);
	$res=$ps->find($conditions=array('id'=>$id));
	if($res[umeta_id]!=0){
		$ms=spClass(site_meta);
		$metares=$ms->find($conditions=array(umeta_id=>$res[umeta_id]));
		$this->meta=$metares;
	}
	if($res['post_summary']!=null){
		$res['post_summary_1']=explode(',',$res['post_summary']);//0rginal
		$res['post_summary_2']=preg_replace('/(\d+)\//U','\1/thumb/',explode(',',$res['post_summary']));//thumb
	}
	$this->res=$res;
	$this->display('management/article_edit.html');
}else if($_REQUEST['act']&&$_REQUEST['act']=='add'){
	$this->display('management/article_add.html');
}else if($_REQUEST['act']&&$_REQUEST['act']=='del'){//执行删除操作
		if(!$_REQUEST['id']){
			$this->error('必须指定需删除文章的ID');
			die;
		}else{
			$id=intval($_REQUEST['id']);
			$ps=spClass(posts);
			$res=$ps->find($conditions=array('id'=>$id), $sort = null, $fields ='system,umeta_id');
			if($res[system]==1){
				$this->error('此页面不能删除!');
			die;
			}else{
				$umeta_id=$res[umeta_id];
				$ps->delete($conditions=array(id=>$id));
				$ms=spClass(site_meta);
				$ms->delete($conditions=array('umeta_id'=>$umeta_id));
				unset($ms);
				$this->success('恭喜，删除成功！');
			}	
		}	
}else if($_REQUEST['display']||$_REQUEST['highlight']||$_REQUEST['top']){//显示 置顶 推荐
			$id=intval($_REQUEST['id']);
			$ps=spClass(posts);
			$res=$ps->find($conditions=array('id'=>$id), $sort = null, $fields ='fid,system');
			if(!$res){
				$this->error('指定记录不存在!');
				die;
			}else if($res[fid]==0&&$res[system]==1){
				$this->error('Invalid Action!');
				die;
			}else{
				$msg='恭喜！操作成功！';
				if($_REQUEST['display']){
					$display=$_REQUEST['display'];
					if($display=='true'){
						$ps->updateField($conditions=array(id=>$id),'post_satus','1');
					}else if($display=='false'){
						$ps->update($conditions=array(id=>$id), $newrow=array('post_highlight'=>0,'sid'=>0,'post_satus'=>0));	
					}				
				}else if($_REQUEST['highlight']&&$res[fid]!=1){
					$highlight=$_REQUEST['highlight'];
					if($highlight=='true'){
						$ps->update($conditions=array(id=>$id), $newrow=array('post_highlight'=>1,'post_satus'=>1)); 
						$msg='操作成功，若尚未为此文章设置图片，请点击推荐后设置！';
					}else if($highlight=='false'){
						$ps->updateField($conditions=array(id=>$id),'post_highlight','0');	
					}					
				}else if($_REQUEST['top']){
					$top=$_REQUEST['top'];
					if($top=='true'){
						$ps->update($conditions=array(id=>$id), $newrow=array('sid'=>10000,'post_satus'=>1));
					}else if($top=='false'){
						$ps->updateField($conditions=array(id=>$id),'sid','0');	
					}					
				}else{
					$this->error('Invalid Action!');
					exit;
				}			
			$this->success($msg);					
			}
}					